<?php
//无刷新图片上传和裁剪控制器
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;


class JcropController extends Controller {
    
    //执行图片上传
    public function upload(){
       
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
        $upload->rootPath = './Public/Uploads/'; // 设置附件上传目录    
        $upload->autoSub = false; // 设置不创建子目录    
        
        // 上传文件
        $info = $upload->upload();
        if(!$info) {
            // 上传错误提示错误信息
            echo "<script>alert('上传失败,原因：".$upload->getError()."')</script>";
        }else{
            // 上传成功
            echo "<script>window.parent.showPic('".$info['photo']['savename']."')</script>";
        }
        
  
        die();
    }
	
	
	
    //执行图片裁剪
    public function doJcrop(){
        
		//接收信息
        $x = $_POST['cx'];
        $y = $_POST['cy'];
        $w = $_POST['cw'];
        $h = $_POST['ch'];
        $pic = $_POST['picname'];
        //执行裁剪
        $image = new \Think\Image(); 
        $image->open('./Public/Uploads/'.$pic);
        //将图片从$x*$y开始裁剪为$w*$h大小，并保存为$pic图片名
        $image->crop($w,$h,$x,$y)->save('./Public/Uploads/'.$pic);
		
    }
	
	
}